package com.koron.sinoprobe.web.engineering.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.*;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.engineering.bean.po.SpBudgetReview;
import com.koron.sinoprobe.web.engineering.bean.po.SpEstablish;
import com.koron.sinoprobe.web.engineering.bean.query.SpBudgetReviewQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpBudgetReviewVO;
import com.koron.sinoprobe.web.engineering.service.SpBudgetReviewService;
import com.koron.sinoprobe.web.engineering.workflow.StaffHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预算审查(SpBudgetReview)表控制层
 *
 * @author jzp
 * @since 2021-11-30 10:06:49
 */
@Api(tags = "预算审查接口")
@RestController
@RequestMapping("sp/budgetReview")
public class SpBudgetReviewController implements WorkflowController, WorkflowPluginController {
    /**
     * 服务对象
     */
    @Autowired
    private SpBudgetReviewService spBudgetReviewService;
    @Autowired
    private StaffHelper staffHelper;
    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<SpBudgetReviewVO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "getById", SpBudgetReviewVO.class, id));
    }

    /**
     * 根据项目id查询最新一条
     *
     * @param projectId 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据项目id查询")
    @GetMapping("getByProjectId")
    public Response<SpEstablish> selectOneByProjectId(String projectId) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "getByProjectId", SpEstablish.class, projectId));
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<PageInfo<SpBudgetReviewVO>> page(PageQuery pageQuery, SpBudgetReviewQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "保存或更新")
    @PostMapping("save")
    public Response<String> save(@RequestBody SpBudgetReview spBudgetReview) {
        ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "saveOrUpdate", Boolean.class, spBudgetReview);
        return Response.ok(spBudgetReview.getId());
    }

    @ApiOperation(value = "提交")
    @PostMapping("submit")
    public Response<String> submit(@RequestBody SpBudgetReview spBudgetReview) {
        ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "submit", Boolean.class, spBudgetReview);
        return Response.ok(spBudgetReview.getId());
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    /**
     * 工作溜
     */
    @Override
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        // 提交
        if (redirectQuery.getOperation() == 32) {
            SpBudgetReview spBudgetReview = JSON.parseObject(redirectQuery.getFormVariables(), SpBudgetReview.class);
            Assert.notNull(spBudgetReview, "formVariables参数转化为SpBudgetReview类失败");
            // 如果是结束
            if (redirectQuery.getCurNodeCode().equals("END")) {
                ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "end", Boolean.class, spBudgetReview);
            } else {
                // 否则为提交
                ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "submit", Boolean.class, spBudgetReview);
            }
        } else if ((redirectQuery.getOperation() == 1 && redirectQuery.getCurNodeCode().startsWith("draft-")) || redirectQuery.getOperation() == 256) {
            // 如果是驳回或撤回，回到草稿状态
            ADOConnection.runTask(getCurrDs(), spBudgetReviewService, "updateStatus", Boolean.class, redirectQuery.getDataId(), Constants.PackageStatus.DRAFT);
        }
        return Response.success();
    }

    /**
     * 取交集
     */
    @Override
    public Response<List<WorkflowUser>> submitter(@RequestBody SubmitterParam submitterParam) {
        return Response.ok(staffHelper.mixed(submitterParam.getUsers()));
    }

    @Override
    public Response<List<WorkflowUser>> handler(@RequestBody SubmitterParam submitterParam) {
        return Response.ok();
    }

    /**
     * 添加编制人所在部门
     */
    @ApiOperation(value = "handler")
    @PostMapping("creatorOrg/"+handler)
    public Response<List<WorkflowUser>> creatorOrgHandler(@RequestBody SubmitterParam submitterParam) {
        staffHelper.fillAuthorizerOrg(submitterParam, getAuthorizer(submitterParam.getFormVariables()));
        return Response.ok(staffHelper.mixed(submitterParam.getUsers()));
    }

    /**
     * 添加编制人所在部门
     */
    @PostMapping("creatorOrg/"+submitter)
    public Response<List<WorkflowUser>> creatorOrgSubmitter(@RequestBody SubmitterParam submitterParam) {
        staffHelper.fillAuthorizerOrg(submitterParam, getAuthorizer(submitterParam.getFormVariables()));
        return Response.ok(staffHelper.mixed(submitterParam.getUsers()));
    }

    private String getAuthorizer(String formVariables) {
        // 1. 获取编制人信息
        SpBudgetReview spBudgetReview = JSONObject.parseObject(formVariables, SpBudgetReview.class);
        // 编制人账号
        return spBudgetReview.getAuthorizer();
    }

}

